package com.live.shop.home;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.live.shop.BaseActivity;
import com.live.shop.R;
import com.live.shop.home.keyword.AddKeywordActivity;
import com.live.shop.model.KeywordModel;
import com.live.shop.model.UserModel;
import com.live.shop.sql.KeywordListDatabase;
import com.live.shop.sql.UserDatabase;
import com.live.shop.tool.StatusBarUtils;

import java.util.List;

public class KeywordsListActivity extends BaseActivity {
    ImageButton keyword_back;
    TextView keyword_add;
    RecyclerView keyword_recyclerview;
    List<KeywordModel> list;
    KeywordListDatabase keyDb;
    UserModel userModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_keywords_list);
        StatusBarUtils.setDarkStatusWhite(this,R.color.white);

        UserDatabase userDb = new UserDatabase(getApplicationContext());
        userModel = userDb.select();

        keyword_back = findViewById(R.id.keyword_back);
        keyword_add = findViewById(R.id.keyword_add);
        keyword_recyclerview = findViewById(R.id.keyword_recyclerview);

        keyword_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        keyword_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getApplicationContext(), AddKeywordActivity.class));
            }
        });
    }
    public void initData(){
        keyDb = new KeywordListDatabase(getApplicationContext());
        list = keyDb.selectAll();
        adapter = new MyAdapter();
        keyword_recyclerview.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        keyword_recyclerview.setAdapter(adapter);
    }
    private MyAdapter adapter;

    class MyAdapter extends RecyclerView.Adapter {
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyAdapter.MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_keyword, parent, false));
        }
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
            MyAdapter.MyViewHolder mholder = (MyAdapter.MyViewHolder) holder;
            KeywordModel keywordModel = list.get(position);

            if(keywordModel.getKeywordStatus()==1){
                mholder.item_keyword_status.setSelected(true);
                mholder.item_keyword_status_txt.setText("启用中");
                mholder.item_keyword_status_txt.setTextColor(getColor(R.color.blue));
            }else{
                mholder.item_keyword_status.setSelected(false);
                mholder.item_keyword_status_txt.setText("未启用");
                mholder.item_keyword_status_txt.setTextColor(getColor(R.color.gary));
            }

            mholder.item_keyword_question.setText("问题："+keywordModel.getQuestion());
            mholder.item_keyword_key.setText("关键词："+keywordModel.getKeyword());
            mholder.item_keyword_use_num.setText(String.valueOf(keywordModel.getUseNum()));
            mholder.item_keyword_answer_num.setText(String.valueOf(keywordModel.getListRecord().size()));
            mholder.item_keyword_create_time.setText(keywordModel.getCreateTime());

            mholder.item_keyword_status.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(keywordModel.getKeywordStatus() == 1){
                        //不启用
                        list.get(position).setKeywordStatus(0);
                        keyDb.updateKeywordStatus(keywordModel.getKeywordId(),0);

                    }else{
                        //启用
                        list.get(position).setKeywordStatus(1);
                        keyDb.updateKeywordStatus(keywordModel.getKeywordId(),1);
                    }
                    notifyDataSetChanged();
                }
            });
            mholder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getApplicationContext(), AddKeywordActivity.class);
                    intent.putExtra("data",keywordModel);
                    intent.putExtra("type", 1);
                    startActivity(intent);
                }
            });
            mholder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    dialog_item_delete(holder.getBindingAdapterPosition());
                    return true;
                }
            });
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView item_keyword_question;
            ImageView item_keyword_status;
            TextView item_keyword_key;
            TextView item_keyword_status_txt;
            TextView item_keyword_use_num;
            TextView item_keyword_answer_num;
            TextView item_keyword_create_time;

            public MyViewHolder(View itemView) {
                super(itemView);
                item_keyword_question = itemView.findViewById(R.id.item_keyword_question);
                item_keyword_status = itemView.findViewById(R.id.item_keyword_status);
                item_keyword_key = itemView.findViewById(R.id.item_keyword_key);
                item_keyword_status_txt = itemView.findViewById(R.id.item_keyword_status_txt);
                item_keyword_use_num = itemView.findViewById(R.id.item_keyword_use_num);
                item_keyword_answer_num = itemView.findViewById(R.id.item_keyword_answer_num);
                item_keyword_create_time = itemView.findViewById(R.id.item_keyword_create_time);
            }
        }
    }
    public void dialog_item_delete(int position){
        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setCancelable(false);
        dialog.show();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_item_delete);
        final TextView diaglog_delete_ok = window.findViewById(R.id.diaglog_delete_ok);
        final TextView diaglog_delete_no = window.findViewById(R.id.diaglog_delete_no);
        diaglog_delete_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                adapter.notifyItemRemoved(position);
                keyDb.deleteData(list.get(position).getKeywordId());
                list.remove(position);
                dialog.dismiss();
            }
        });
        diaglog_delete_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }
}